﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Common;
using Dal;


namespace ResultManagement
{
    public class ResultManager
    {
        string _connectionString;
        string _prefixFilePath;

        public ResultManager(string ConnectionString, string PrefixFilePath)
        {
            _connectionString = ConnectionString;
            _prefixFilePath = PrefixFilePath;
        }

        //Returns only documents matching one of the doc types specified.
        public List<Document> FilterResultsByDocumentTypes(List<Document> docs, List<Document.Type> docTypes)
        {
            List<Document> res = new List<Document>();
            foreach (Document doc in docs)
            {
                if (docTypes.Contains(doc.DocType))
                    res.Add(doc);
            }
            return res;
        }

        //Gets list of documents including excerpts for all of the user's queries in category.
        public List<Document> GetUserCategoryResults(string userName, Category category, bool IsRead)
        {

            Hashtable DocQueries = new Hashtable();
            DatabaseAbstractionLayer Dal = new DatabaseAbstractionLayer(_connectionString);
            User user = Dal.GetUserByUserName(userName);
            HebrewStemmer hs = new HebrewStemmer(_prefixFilePath);

            //For each user query, get list of documents that were a match.
            foreach (string query in category.Queries)
            {
                List<Document> results = Dal.GetUserSearchResultsByQuery(user, query, IsRead);

                //Make a hash of - Key: Document, Value: Query List within this document.
                //This is for displaying multiple query results in one document.
                foreach (Document doc in results)
                {
                    if (!category.IsDocumentTypeRequested(doc.DocType)) continue;
                    if (DocQueries.ContainsKey(doc))
                    {
                        if (DocQueries[doc] as List<string> == null)
                        {
                            List<string> queryList = new List<string>();
                            queryList.Add(query);
                            DocQueries[doc] = queryList;
                        }
                        else
                            (DocQueries[doc] as List<string>).Add(query);
                    }
                    else
                    {
                        List<string> queryList = new List<string>();
                        queryList.Add(query);
                        DocQueries.Add(doc, queryList);
                    }
                }
            }

            List<Document> result = new List<Document>();
            //Each document gets its excerpts (according to the queries it matches).
            foreach (DictionaryEntry e in DocQueries)
            {
                Document doc = e.Key as Document;

                //Get Excerpts for each document
                doc.Excerpts =
                    doc.GetHTMLExcerpts(((List<string>)DocQueries[doc]).ToArray(),
                    Document.Section.Text, 1, 7, 7, hs);

                //Get Display Name for each document
                doc.DisplayNameProperty = doc.GetDocumentDisplayName(Dal.GetSiteNames(doc.SiteName));
                //Get Priority for this category.
                doc.SitePriority = category.GetPriorityBySiteName(doc.SiteName);
                result.Add(doc);
            }

            return result;
        }

        
    }
}
